package com.p2p.front.service.impl;

import java.util.Calendar;

import javax.annotation.Resource;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.p2p.front.dal.dao.EmailCodeDao;
import com.p2p.front.dal.model.EmailCode;
import com.p2p.front.service.EmailCodeService;

@Service
public class EmailCodeServiceImpl implements EmailCodeService {
	
	@Resource private EmailCodeDao		emailCodeDao;

	@Override
	@Transactional
	public void saveEmailCode(EmailCode emailCode) {
		emailCodeDao.save(emailCode);
	}

	@Override
	public EmailCode getEmailCodeById(String emailCodeId) {
		return emailCodeDao.getEmailCodeById(emailCodeId);
	}

	@Override
	@Transactional
	public void updateEmailCode(EmailCode emailCode) {
		emailCodeDao.updateEmailCode(emailCode);
	}

	@Override
	public EmailCode saveEmailCode(String emailAddress, String code, Integer expireHours,int sendType) {
		EmailCode emailCode = new EmailCode();
		emailCode.setEmail(emailAddress);
		emailCode.setCode(code);
		emailCode.setType(emailAddress.substring(emailAddress.lastIndexOf('@') + 1));
		emailCode.setStatus(EmailCode.Status.INVALID.getStatus());
		emailCode.setExpire(DateUtils.addHours(Calendar.getInstance().getTime(),expireHours).getTime());
		emailCode.setSendEmailType(sendType);
		saveEmailCode(emailCode);
		return emailCode;
	}
}
